package com.xbongbong.saas.service.impl;/*/*/

import com.xbongbong.i18n.util.I18nMessageUtil;
import com.xbongbong.paas.enums.DelEnum;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.pro.constant.I18nStringConstant;
import com.xbongbong.paas.toolbox.util.DateTimeUtil;
import com.xbongbong.pro.menu.pojo.dto.MenuInitDTO;
import com.xbongbong.saas.domain.entity.WorkOrderTemplateCategoryEntity;
import com.xbongbong.saas.model.WorkOrderTemplateCategoryModel;
import com.xbongbong.saas.service.WorkOrderTemplateCategoryService;
import com.xbongbong.sys.domain.entity.UserEntity;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 *
 * @author 魏荣杰
 * @date 2019/3/13 16:10
 * @since v1.0
 * @version v1.0
 */
@Service("workOrderTemplateCategoryService")
public class WorkOrderTemplateCategoryServiceImpl implements WorkOrderTemplateCategoryService {
    @Resource
    private WorkOrderTemplateCategoryModel workOrderTemplateCategoryModel;

    @Override
    public Long initTemplateCategory(MenuInitDTO menuInitDTO, UserEntity userEntity) throws XbbException {
        Long appId = menuInitDTO.getAppId();
        String corpid = menuInitDTO.getCorpid();

        WorkOrderTemplateCategoryEntity workOrderTemplateCategoryEntity = new WorkOrderTemplateCategoryEntity();
        workOrderTemplateCategoryEntity.setCorpid(corpid);
        workOrderTemplateCategoryEntity.setAppId(appId);
        workOrderTemplateCategoryEntity.setName(I18nMessageUtil.getMessage(I18nStringConstant.DEFAULT_CATEGORY));
        workOrderTemplateCategoryEntity.setIsDefault(1);
        workOrderTemplateCategoryEntity.setSort(0);

        workOrderTemplateCategoryEntity.setCreatorId(userEntity.getUserId());
        workOrderTemplateCategoryEntity.setCreatorName(userEntity.getName());
        workOrderTemplateCategoryEntity.setModifierId(userEntity.getUserId());
        workOrderTemplateCategoryEntity.setModifierName(userEntity.getName());
        workOrderTemplateCategoryEntity.setDel(0);
        workOrderTemplateCategoryModel.insert(workOrderTemplateCategoryEntity);
        return workOrderTemplateCategoryEntity.getId();
    }

    @Override
    public Long saveDefaultCategory(Long appId, UserEntity userEntity) throws XbbException {
        Map<String, Object> param = new HashMap<>(16);
        param.put("corpid", userEntity.getCorpid());
        param.put("del", 0);
        Integer count = workOrderTemplateCategoryModel.getEntitysCount(param);
        WorkOrderTemplateCategoryEntity workOrderTemplateCategoryEntity = new WorkOrderTemplateCategoryEntity();
        workOrderTemplateCategoryEntity.setCorpid(userEntity.getCorpid());
        workOrderTemplateCategoryEntity.setAppId(appId);
        workOrderTemplateCategoryEntity.setName(I18nMessageUtil.getMessage(I18nStringConstant.DEFAULT_CATEGORY));
        workOrderTemplateCategoryEntity.setIsDefault(1);
        workOrderTemplateCategoryEntity.setSort(count);

        workOrderTemplateCategoryEntity.setCreatorId(userEntity.getUserId());
        workOrderTemplateCategoryEntity.setCreatorName(userEntity.getName());
        workOrderTemplateCategoryEntity.setModifierId(userEntity.getUserId());
        workOrderTemplateCategoryEntity.setModifierName(userEntity.getName());
        workOrderTemplateCategoryEntity.setDel(0);
        workOrderTemplateCategoryModel.insert(workOrderTemplateCategoryEntity);
        return workOrderTemplateCategoryEntity.getId();
    }

    @Override
    public Long updateTemplateCategory(List<WorkOrderTemplateCategoryEntity> entitys, UserEntity userEntity,MenuInitDTO menuInitDTO) {
        Long defaultID =0L;
        for (WorkOrderTemplateCategoryEntity entity : entitys) {
            if (Objects.equals(entity.getIsDefault(),1)){
                entity.setName("默认分类");
                entity.setSort(0);
                entity.setCreatorId(userEntity.getUserId());
                entity.setCreatorName(userEntity.getName());
                entity.setModifierId(userEntity.getUserId());
                entity.setModifierName(userEntity.getName());
                long now = DateTimeUtil.getInt();
                entity.setUpdateTime(now);
                entity.setDel(0);
                defaultID=entity.getId();
            }else{
                entity.setDel(DelEnum.DELETE.getDel());
            }
        }
        try {
            if (Objects.equals(defaultID,0L)){
                defaultID = initTemplateCategory(menuInitDTO,userEntity);
            }else{
                workOrderTemplateCategoryModel.updateBatch(entitys,menuInitDTO.getCorpid());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return defaultID;
    }
}
